package com.ostrich.module_main.ui

import android.content.ComponentName
import android.content.pm.PackageManager
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.ostrich.library_base.BaseActivity
import com.ostrich.library_common.route.RouterFragmentPath
import com.ostrich.library_common.utils.LoggerUtils
import com.ostrich.module_main.R
import com.ostrich.module_main.adapter.MainPagerAdapter
import com.ostrich.module_main.databinding.MainActivityMainBinding
import com.tencent.mmkv.MMKV
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener


class MainActivity : BaseActivity<MainActivityMainBinding>() {
    private val list:MutableList<Fragment> = mutableListOf()
    private lateinit var mainPagerAdapter:MainPagerAdapter
    override fun initView() {
        MMKV.defaultMMKV().encode("gone",true)//记录执行过主页面，下次打开不显示
    }

    override fun initData() {
//        //状态栏
//        ImmersionBar.with(this)
//            .statusBarView(binding?.topView)
//            .init()

        //获得activity信息
        val activityInfo = packageManager.getActivityInfo(componentName, PackageManager.GET_META_DATA)
        val metaData2 = activityInfo.metaData
        LoggerUtils.d("App",metaData2.getString("test2").toString())



        //ARouter加载fragment
        val homeFragment = ARouter.getInstance().build(RouterFragmentPath.PAGER_HOME).navigation() as Fragment
        val communityFragment = ARouter.getInstance().build(RouterFragmentPath.PAGER_COMMUNITY).navigation() as Fragment
        val moreFragment = ARouter.getInstance().build(RouterFragmentPath.PAGER_MORE).navigation() as Fragment
        val userFragment = ARouter.getInstance().build(RouterFragmentPath.PAGER_USER).navigation() as Fragment
        list.add(homeFragment)
        list.add(communityFragment)
        list.add(moreFragment)
        list.add(userFragment)
        binding?.let {
            mainPagerAdapter = MainPagerAdapter(this,list)
            it.viewPager.adapter = mainPagerAdapter

            val navigationController = it.bottomView.material()
                .addItem(R.drawable.main_home, getString(R.string.main_home), ContextCompat.getColor(this, R.color.black))
                .addItem(R.drawable.main_community,
                    getString(R.string.main_community),
                    ContextCompat.getColor(this, R.color.black))
                .addItem(R.drawable.main_notify,  getString(R.string.main_more), ContextCompat.getColor(this, R.color.black))
                .addItem(R.drawable.main_user,  getString(R.string.main_me), ContextCompat.getColor(this, R.color.black))
                .build()
            navigationController.setHasMessage(2,true)
            navigationController.setMessageNumber(3,8)
            //自动联动：一句话
//            navigationController.setupWithViewPager(it.viewPager)
            //手动实现上下联动：因为PageNavigationView只能和viewpager自动联动
            navigationController.addTabItemSelectedListener(object : OnTabItemSelectedListener {
                override fun onSelected(index: Int, old: Int) {
                    it.viewPager.currentItem = index
                }

                override fun onRepeat(index: Int) {
                }
            })

            it.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    navigationController.setSelect(position)
                }
            })



        }

    }

    override fun initListener() {

    }

    override fun bindLayout(): Int {
       return R.layout.main_activity_main
    }


}